package com.zyh.homework;

import java.util.Scanner;

public class Test8 {
    public static void main(String[] args) {
        /*
        1.键盘录入10个整数存入数组中
	    2.定义一个方法将奇数放在数组的左侧,偶数放在数组的右侧
	    3.定义一个方法打印原数组和处理后的数组
         */
        int[] arr = new int[10];
        for (int i = 0; i < 10; i++) {
            System.out.println("请输入第" + (i + 1) + "个数据：");
            arr[i] = new Scanner(System.in).nextInt();
        }
        int[] changeArray1 = changeArray1(arr);
        printArray(arr);
        System.out.println();
        printArray(changeArray1);

    }

    //    交换数组2
    public static int[] changeArray1(int[] arr) {
        int[] newArr = new int[arr.length];
        int start = 0, end = arr.length - 1, index = 0;
        while (start <= end) {
            if (arr[index] % 2 == 0) {//偶数
                newArr[end] = arr[index];
                end--;
            } else if (arr[index] % 2 != 0) {
                newArr[start] = arr[index];
                start++;
            }
            index++;
        }
        return newArr;
    }

    //    交换数组1
    public static int[] changeArray(int[] arr) {
        int[] newArr = new int[arr.length];
        int index = 0;
        for (int start = 0, end = arr.length - 1; start <= end; ) {
            if (arr[index] % 2 == 1) {//奇数
                newArr[start] = arr[index];
                start++;
            } else if (arr[index] % 2 == 0) {
                newArr[end] = arr[index];
                end--;
            }
            index++;
        }
        return newArr;
    }


    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                System.out.print(arr[i]);
            } else {
                System.out.print(arr[i] + ",");
            }
        }
        System.out.print("]");
    }
}
